package com.rotsel.mh3udb.app.DataSource;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

/**
 * Created by Roman on 17.05.2014.
 */
public class DataManagerOpenHelper extends SQLiteOpenHelper {

    public DataManagerOpenHelper(Context context){
        super(context, "MH3U.db", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL("create table MainMenu (" +
                "[Id] integer NOT NULL," +
                "[Name] text NOT NULL," +
                "[Key] text NOT NULL," +
                "[IsActive] Boolean NOT NULL)");
        database.execSQL("create table WeaponTypes (" +
                "[Id] integer NOT NULL," +
                "[Name] text NOT NULL," +
                "[ShortName] text NOT NULL)");
        database.execSQL("create table Elements (" +
                "[Id] integer NOT NULL," +
                "[Name] text NOT NULL)");
        database.execSQL("create table Weapons (" +
                "[Id] integer NOT NULL," +
                "[Name] text NOT NULL," +
                "[WeaponTypeId] integer NOT NULL," +
                "[WeaponTreeId] integer NOT NULL," +
                "[Rarity] integer NOT NULL," +
                "[WeaponUpgradeId] integer NOT NULL," +
                "[RawDamage] integer NOT NULL," +
                "[BuyingPrice] integer NOT NULL," +
                "[SellingPrice] integer NOT NULL," +
                "[Affinity] integer NULL," +
                "[Defense] integer NULL," +
                "[SlotCount] integer NOT NULL," +
                "[Description] text NOT NULL," +
                "[Sharpness] text NOT NULL," +
                "[SharpnessUp] text NOT NULL," +
                "[ElementId] integer NULL," +
                "[ElementDamage] integer NULL," +
                "[AwakenElementId] integer NULL," +
                "[AwakenElementDamage] integer NULL," +
                "[SecondElementId] integer NULL," +
                "[SecondElementDamage] integer NULL," +
                "[SecondAwakenElementId] integer NULL," +
                "[SecondAwakenElementDamage] integer NULL," +
                "[ShellingId] integer NULL," +
                "[ShellingLvl] integer NULL," +
                "[PhialId] integer NULL," +
                "[ReloadId] integer NULL," +
                "[RecoilId] integer NULL," +
                "[DeviationId] integer NULL," +
                "[BarrelDamage] integer NULL)");

        insertTestData(database);
    }

    private void insertTestData(SQLiteDatabase database) {
        insertTestMainMenuData(database);
        insertTestWeaponTypesData(database);
        insertTestElementsData(database);
        insertTestWeaponsData(database);
    }

    private void insertTestWeaponsData(SQLiteDatabase database) {
        ContentValues values;
        values = new ContentValues();
        values.put("Id", 1);
        values.put("Name", "Iron Katana");
        values.put("WeaponTypeId", 7);
        values.put("WeaponTreeId", 0);
        values.put("Rarity", 1);
        values.put("WeaponUpgradeId", 0);
        values.put("RawDamage", 264);
        values.put("BuyingPrice", 1500);
        values.put("SellingPrice", 750);
        values.put("Affinity", "NULL");
        values.put("Defense", "NULL");
        values.put("SlotCount", 0);
        values.put("Description", "A Long Sword forged with Eastern methods. Durable and resilient, but requires regular upkeep.");
        values.put("Sharpness", "15,9,20,0,0,0,0");
        values.put("SharpnessUp", "15,9,26,4,0,0,0");
        values.put("ElementId", 4);
        values.put("ElementDamage", 50);
        values.put("AwakenElementId", "NULL");
        values.put("AwakenElementDamage", "NULL");
        values.put("SecondElementId", "NULL");
        values.put("SecondElementDamage", "NULL");
        values.put("SecondAwakenElementId", "NULL");
        values.put("SecondAwakenElementDamage", "NULL");
        values.put("ShellingId", "NULL");
        values.put("ShellingLvl", "NULL");
        values.put("PhialId", "NULL");
        values.put("ReloadId", "NULL");
        values.put("RecoilId", "NULL");
        values.put("DeviationId", "NULL");
        values.put("BarrelDamage", "NULL");
        database.insert("Weapons", null, values);

        values = new ContentValues();
        values.put("Id", 2);
        values.put("Name", "Iron Grace");
        values.put("WeaponTypeId", 7);
        values.put("WeaponTreeId", 0);
        values.put("Rarity", 1);
        values.put("WeaponUpgradeId", 1);
        values.put("RawDamage", 330);
        values.put("BuyingPrice", 2100);
        values.put("SellingPrice", 1050);
        values.put("Affinity", "NULL");
        values.put("Defense", "NULL");
        values.put("SlotCount", 0);
        values.put("Description", "No description");
        values.put("Sharpness", "9,13,17,4,0,0,0");
        values.put("SharpnessUp", "9,13,17,15,0,0,0");
        values.put("ElementId", 4);
        values.put("ElementDamage", 70);
        values.put("AwakenElementId", "NULL");
        values.put("AwakenElementDamage", "NULL");
        values.put("SecondElementId", "NULL");
        values.put("SecondElementDamage", "NULL");
        values.put("SecondAwakenElementId", "NULL");
        values.put("SecondAwakenElementDamage", "NULL");
        values.put("ShellingId", "NULL");
        values.put("ShellingLvl", "NULL");
        values.put("PhialId", "NULL");
        values.put("ReloadId", "NULL");
        values.put("RecoilId", "NULL");
        values.put("DeviationId", "NULL");
        values.put("BarrelDamage", "NULL");
        database.insert("Weapons", null, values);
    }

    private void insertTestElementsData(SQLiteDatabase database) {
        ContentValues values;
        values = new ContentValues();
        values.put("Id", 1);
        values.put("Name", "Fire");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 2);
        values.put("Name", "Water");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 3);
        values.put("Name", "Thunder");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 4);
        values.put("Name", "Ice");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 5);
        values.put("Name", "Dragon");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 6);
        values.put("Name", "Paralysis");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 7);
        values.put("Name", "Poison");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 8);
        values.put("Name", "Sleep");
        database.insert("Elements", null, values);

        values = new ContentValues();
        values.put("Id", 9);
        values.put("Name", "Slimeblight");
        database.insert("Elements", null, values);
    }

    private void insertTestWeaponTypesData(SQLiteDatabase database) {
        ContentValues values;
        values = new ContentValues();
        values.put("Id", 1);
        values.put("Name", "Sword and Shield");
        values.put("ShortName", "SnS");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 2);
        values.put("Name", "Dual Swords");
        values.put("ShortName", "DS");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 3);
        values.put("Name", "Light Bowgun");
        values.put("ShortName", "LBG");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 4);
        values.put("Name", "Heavy Bowgun");
        values.put("ShortName", "HBG");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 5);
        values.put("Name", "Bow");
        values.put("ShortName", "Bow");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 6);
        values.put("Name", "Great Sword");
        values.put("ShortName", "GS");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 7);
        values.put("Name", "Long Sword");
        values.put("ShortName", "LS");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 8);
        values.put("Name", "Lance");
        values.put("ShortName", "L");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 9);
        values.put("Name", "Gunlance");
        values.put("ShortName", "GL");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 10);
        values.put("Name", "Hammer");
        values.put("ShortName", "H");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 11);
        values.put("Name", "Hunting Horn");
        values.put("ShortName", "HH");
        database.insert("WeaponTypes", null, values);

        values = new ContentValues();
        values.put("Id", 12);
        values.put("Name", "Switch Axe");
        values.put("ShortName", "SA");
        database.insert("WeaponTypes", null, values);
    }

    private void insertTestMainMenuData(SQLiteDatabase database) {
        ContentValues values;
        values = new ContentValues();
        values.put("Id", 1);
        values.put("Name", "Weapon List");
        values.put("Key", "0");
        values.put("IsActive", 1);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 2);
        values.put("Name", "Weapon Tree");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 3);
        values.put("Name", "Armour list");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 4);
        values.put("Name", "Armour sets");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 5);
        values.put("Name", "Maps");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 6);
        values.put("Name", "Decorations");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 7);
        values.put("Name", "Materials");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 8);
        values.put("Name", "Quests");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 9);
        values.put("Name", "Monsters");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);

        values = new ContentValues();
        values.put("Id", 10);
        values.put("Name", "Combination list");
        values.put("Key", "0");
        values.put("IsActive", 0);
        database.insert("MainMenu", null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF exists MainMenu");
        database.execSQL("DROP TABLE IF exists WeaponTypes");
        database.execSQL("DROP TABLE IF exists Elements");
        database.execSQL("DROP TABLE IF exists Weapons");
        onCreate(database);
    }
}
